{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 老师的代码0-5已经做了的就不再赘述了，结果文件直接用就行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of users in train & test :3391\n",
      "number of items in train & test :13418\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "\n",
    "import pickle\n",
    "import numpy as np\n",
    "import scipy.io as sio\n",
    "import scipy.sparse as ss\n",
    "from numpy.random import random  \n",
    "from collections import defaultdict\n",
    "#相似度/距离\n",
    "import scipy.spatial.distance as ssd\n",
    "\n",
    "dataPath = '../JupyterData/'\n",
    "#读取训练集和测试集中出现过的用户和活动列表\n",
    "userIndex = pickle.load(open(\"PE_userIndex.pkl\", 'rb'))\n",
    "eventIndex = pickle.load(open(\"PE_eventIndex.pkl\", 'rb'))\n",
    "#读取在测试集和训练集中出现的用户对\n",
    "uniqueUserPairs = pickle.load(open(\"FE_uniqueUserPairs.pkl\", 'rb'))\n",
    "#读取用户对所有活动的打分矩阵\n",
    "userEventScores = sio.mmread(\"PE_userEventScores\").todense()\n",
    "#读取在测试集和训练集中出现的活动对\n",
    "uniqueEventPairs = pickle.load(open(\"PE_uniqueEventPairs.pkl\", 'rb'))\n",
    "n_users = len(userIndex)\n",
    "n_items = len(eventIndex)\n",
    "print(\"number of users in train & test :%d\" % n_users)\n",
    "print(\"number of items in train & test :%d\" % n_items)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 下面补全作业要求的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将所有特征串联起来，构成RS_Train.csv\n",
    "#RS_Test.csv\n",
    "#为最后推荐系统做准备\n",
    "\n",
    "class RecommonderSystem:\n",
    "  def __init__(self):\n",
    "    # 读入数据做初始化\n",
    "    \n",
    "    #用户和活动新的索引\n",
    "    self.userIndex = userIndex\n",
    "    self.eventIndex =eventIndex\n",
    "    self.n_users = n_users\n",
    "    self.n_items = n_items\n",
    "    \n",
    "    #用户-活动关系矩阵R\n",
    "    #在train_SVD会重新从文件中读取,二者要求的格式不同，来不及统一了:(\n",
    "    self.userEventScores = userEventScores\n",
    "    \n",
    "    #倒排表\n",
    "    ##每个用户参加的事件\n",
    "    self.itemsForUser = pickle.load(open(\"PE_eventsForUser.pkl\", 'rb'))\n",
    "    ##事件参加的用户\n",
    "    self.usersForItem = pickle.load(open(\"PE_usersForEvent.pkl\", 'rb'))\n",
    "    \n",
    "    #基于模型的协同过滤参数初始化,训练\n",
    "    self.init_SVD()\n",
    "    self.train_SVD(trainfile = \"train.csv\")\n",
    "    \n",
    "    #根据用户属性计算出的用户之间的相似度\n",
    "    self.userSimMatrix = sio.mmread(\"US_userSimMatrix\").todense()\n",
    "    \n",
    "    #根据活动属性计算出的活动之间的相似度\n",
    "    self.eventPropSim = sio.mmread(\"EV_eventPropSim\").todense()\n",
    "    self.eventContSim = sio.mmread(\"EV_eventContSim\").todense()\n",
    "    \n",
    "    #每个用户的朋友的数目\n",
    "    self.numFriends = sio.mmread(\"UF_numFriends\")\n",
    "    #用户的每个朋友参加活动的分数对该用户的影响\n",
    "    self.userFriends = sio.mmread(\"UF_userFriends\").todense()\n",
    "    \n",
    "    #活动本身的热度\n",
    "    self.eventPopularity = sio.mmread(\"EA_eventPopularity\").todense()\n",
    "\n",
    "  def init_SVD(self, K=20):\n",
    "    #初始化模型参数（for 基于模型的协同过滤SVD_CF）\n",
    "    self.K = K  \n",
    "    \n",
    "    #init parameters\n",
    "    #bias\n",
    "    self.bi = np.zeros(self.n_items)  \n",
    "    self.bu = np.zeros(self.n_users)  \n",
    "    \n",
    "    #the small matrix\n",
    "    self.P = random((self.n_users,self.K))/10*(np.sqrt(self.K))\n",
    "    self.Q = random((self.K, self.n_items))/10*(np.sqrt(self.K))  \n",
    "                  \n",
    "          \n",
    "  def train_SVD(self,trainfile = 'train.csv', steps=100,gamma=0.04,Lambda=0.15):\n",
    "    #训练SVD模型（for 基于模型的协同过滤SVD_CF）\n",
    "    #gamma：为学习率\n",
    "    #Lambda：正则参数\n",
    "    \n",
    "    #偷懒了，为了和原来的代码的输入接口一样，直接从训练文件中去读取数据\n",
    "    print(\"SVD Train...\") \n",
    "    ftrain = open(dataPath+trainfile, 'r')\n",
    "    ftrain.readline()\n",
    "    self.mu = 0.0\n",
    "    n_records = 0\n",
    "    uids = []  #每条记录的用户索引\n",
    "    i_ids = [] #每条记录的item索引\n",
    "    #用户-Item关系矩阵R（内容同userEventScores相同），临时变量，训练完了R不再需要\n",
    "    R = np.zeros((self.n_users, self.n_items))\n",
    "    \n",
    "    for line in ftrain:\n",
    "        cols = line.strip().split(\",\")\n",
    "        u = self.userIndex[cols[0]]  #用户\n",
    "        i = self.eventIndex[cols[1]] #活动\n",
    "        \n",
    "        uids.append(u)\n",
    "        i_ids.append(i)\n",
    "        \n",
    "        R[u,i] = int(cols[4])  #interested\n",
    "        self.mu += R[u,i]\n",
    "        n_records += 1\n",
    "    \n",
    "    ftrain.close()\n",
    "    self.mu /= n_records\n",
    "    \n",
    "    # 请补充完整SVD模型训练过程(已补完)\n",
    "    for i in range(steps):      #梯度下降，走100步\n",
    "        for n in range(n_records):    #批量梯度下降，用训练集的全部数据去求梯度\n",
    "            predSocre=self.pred_SVD(uids[n], i_ids[n])    #当前参数的预测分\n",
    "            eui=R[uids[n],i_ids[n]]-predSocre\n",
    "            \n",
    "            #对损失函数求梯度(注意此处不包括正则项，因为梯度下降是为了求损失函数的最小值)\n",
    "            gSSE_Puk=-eui*self.Q[:,i_ids[n]] + Lambda*self.P[uids[n],:]\n",
    "            gSSE_Qki=-eui*self.P[uids[n],:] + Lambda*self.Q[:,i_ids[n]]\n",
    "            gSSE_Bu =-eui+Lambda*self.bu[uids[n]]\n",
    "            gSSE_Bi =-eui+Lambda*self.bi[i_ids[n]]\n",
    "            \n",
    "            #更新参数\n",
    "            self.P[uids[n],:] = self.P[uids[n],:]-gamma*gSSE_Puk\n",
    "            self.Q[:,i_ids[n]] = self.Q[:,i_ids[n]]-gamma*gSSE_Qki\n",
    "            self.bu[uids[n]] = self.bu[uids[n]] -gamma*gSSE_Bu\n",
    "            self.bi[i_ids[n]] = self.bi[i_ids[n]] -gamma*gSSE_Bi\n",
    "            \n",
    "            if i%10==0 and n %5000==0:\n",
    "                print(\"已走{}步,用了{}条训练数据\".format(i,n))\n",
    "                print(\"Puk=\",self.P[uids[n],:])\n",
    "                print(\"Qki=\",self.Q[:,i_ids[n]])\n",
    "                print(\"Bu=\",self.bu[uids[n]])\n",
    "                print(\"Bi=\",self.bi[i_ids[n]])\n",
    "        \n",
    "        \n",
    "    print(\"SVD trained\") \n",
    "    \n",
    "  def pred_SVD(self, uid, i_id):\n",
    "    #根据当前参数，预测用户uid对Item（i_id）的打分        \n",
    "    ans=self.mu + self.bi[i_id] + self.bu[uid] + np.dot(self.P[uid,:],self.Q[:,i_id])  \n",
    "        \n",
    "    #将打分范围控制在0-1之间\n",
    "    if ans>1:  \n",
    "        return 1  \n",
    "    elif ans<0:  \n",
    "        return 0\n",
    "    return ans  \n",
    "\n",
    "  def sim_cal_UserCF(self, uid1, uid2 ):\n",
    "    #请补充基于用户的协同过滤中的两个用户uid1和uid2之间的相似度（根据两个用户对item打分的相似度）(已补充)\n",
    "    similarity = 0.0\n",
    "    \n",
    "    si={}  #有效item（两个用户均有打分的item）的集合\n",
    "    for item in self.itemsForUser[uid1]:  #uid1所有打过分的Item1\n",
    "        if item in self.itemsForUser[uid2]:  #如果uid2也对该Item打过分\n",
    "            si[item]=1  #item为一个有效item\n",
    "        \n",
    "    #print si\n",
    "    n=len(si)   #有效item数，有效item为即对uid对Item打过分，uid2也对Item打过分\n",
    "    if (n==0):  #没有共同打过分的item，相似度设为0？\n",
    "        return similarity  \n",
    "    else:   \n",
    "        #用户uid1打过分的所有有效的item\n",
    "        s1=np.array([self.userEventScores[uid1,item] for item in si])  \n",
    "\n",
    "        #用户uid2打过分的所有有效的Item\n",
    "        s2=np.array([self.userEventScores[uid2,item] for item in si])  \n",
    "\n",
    "        similarity = ssd.correlation(s1,s2)\n",
    "#         sum1=np.sum(s1)                      #E(X)\n",
    "#         sum2=np.sum(s2)                      #E(Y)\n",
    "#         sum1Sq=np.sum(s1**2)                 #E(X²)\n",
    "#         sum2Sq=np.sum(s2**2)                 #E(Y²)\n",
    "#         pSum=np.sum(s1*s2)                   #E(XY)\n",
    "\n",
    "#         #分子 \n",
    "#         num=pSum-(sum1*sum2/n)              #协方差Cov=E(XY)-E(X)E(Y)\n",
    "\n",
    "#         #分母\n",
    "#         den=np.sqrt((sum1Sq-sum1**2/n)*(sum2Sq-sum2**2/n))  #方差D(X)=E(X²)-[E(X)]²  D(Y)=E(Y²)-[E(Y)]²\n",
    "#         if den==0:  \n",
    "#             similarity=0  \n",
    "#             return 0  \n",
    "\n",
    "#         similarity = num/den  \n",
    "        return similarity \n",
    "\n",
    "  def userCFReco(self, userId, eventId):\n",
    "    \"\"\"\n",
    "    根据User-based协同过滤，得到event的推荐度\n",
    "    基本的伪代码思路如下：\n",
    "    for item i\n",
    "      for every other user v that has a preference for i\n",
    "        compute similarity s between u and v\n",
    "        incorporate v's preference for i weighted by s into running aversge\n",
    "    return top items ranked by weighted average\n",
    "    \"\"\"\n",
    "    #请补充完整代码(已补充)\n",
    "    ans = 0.0\n",
    "    i = self.userIndex[userId]            #user的Index\n",
    "    j = self.eventIndex[eventId]          #活动的index\n",
    "\n",
    "    sim_accumulate=0.0  \n",
    "    rat_acc=0.0  \n",
    "\n",
    "    for user in self.usersForItem[j]:  #对eventId打过分的所有用户\n",
    "        sim = self.sim_cal_UserCF(uid1 = user,uid2 = i)    #依次计算该user与其他user之间的相似度\n",
    "        if sim == 0:continue  \n",
    "            \n",
    "        rat_acc += sim * self.userEventScores[user,j]   #其他user对eventId的打分与相似度乘积 的总和\n",
    "        sim_accumulate += sim                   #所有其他user的与此user相似度总和（计算推荐度的分母）\n",
    "        \n",
    "    if sim_accumulate==0:        #分母为0，即没有一个相似的用户，则推荐度返回平均分 \n",
    "        return  self.mu  \n",
    "    ans = rat_acc/sim_accumulate \n",
    "\n",
    "    #将打分范围控制在0-1之间\n",
    "    if ans>1:  \n",
    "        return 1  \n",
    "    elif ans<0:  \n",
    "        return 0\n",
    "    return ans\n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "  def sim_cal_ItemCF(self, i_id1, i_id2):\n",
    "    #计算Item i_id1和i_id2之间的相似性\n",
    "    si={}  #有效用户集合\n",
    "    for user in self.usersForItem[i_id1]:  #所有对Item1打过分的的user\n",
    "        if user in self.usersForItem[i_id2]:  #如果该用户对Item2也打过分\n",
    "            si[user]=1  #user为一个有效用用户\n",
    "        \n",
    "    n=len(si)   #有效用户数，有效用户为即对Item1打过分，也对Item2打过分\n",
    "    if (n==0):  #没有共同打过分的用户，相似度设为0？\n",
    "        return 0  \n",
    "        \n",
    "    #所有有效用户对Item1的打分\n",
    "    s1=np.array([self.userEventScores[u, i_id1] for u in si])  \n",
    "        \n",
    "    #所有有效用户对Item2的打分\n",
    "    s2=np.array([self.userEventScores[u, i_id2] for u in si])  \n",
    "    \n",
    "    similarity = ssd.cosine(s1,s2)    #余弦相似度\n",
    "        \n",
    "    return similarity    \n",
    "            \n",
    "    \n",
    "  def eventCFReco(self, userId, eventId):    \n",
    "    \"\"\"\n",
    "    根据基于物品的协同过滤，得到Event的推荐度\n",
    "    基本的伪代码思路如下：\n",
    "    for item i \n",
    "        for every item j tht u has a preference for\n",
    "            compute similarity s between i and j\n",
    "            add u's preference for j weighted by s to a running average\n",
    "    return top items, ranked by weighted average\n",
    "    \"\"\"\n",
    "    u = self.userIndex[userId]\n",
    "    i = self.eventIndex[eventId]\n",
    "\n",
    "    sim_accumulate=0.0  \n",
    "    rat_acc=0.0  \n",
    "                   \n",
    "    for item in self.itemsForUser[u]:  #用户uid打过分的所有Item\n",
    "        sim = self.sim_cal_ItemCF(item,i)    #依次循环计算该Item与i_id之间的相似度\n",
    "           \n",
    "        rat_acc += sim * self.userEventScores[u,item]   #所有相似item与其被此用户评分的乘积 总和\n",
    "        sim_accumulate += sim       #相似度总和\n",
    "        \n",
    "    if sim_accumulate==0: #无相似item，返回平均分  \n",
    "        return  self.mu  \n",
    "\n",
    "    ans = rat_acc/sim_accumulate  \n",
    "\n",
    "    #将打分范围控制在0-1之间\n",
    "    if ans>1:  \n",
    "        return 1  \n",
    "    elif ans<0:  \n",
    "        return 0\n",
    "    return ans\n",
    "    \n",
    "  def svdCFReco(self, userId, eventId):\n",
    "    #基于模型的协同过滤, SVD++/LFM\n",
    "    u = self.userIndex[userId]\n",
    "    i = self.eventIndex[eventId]\n",
    "\n",
    "    return self.pred_SVD(u,i)\n",
    "\n",
    "  def userReco(self, userId, eventId):\n",
    "    \"\"\"\n",
    "    类似基于User-based协同过滤，只是用户之间的相似度由用户本身的属性得到，计算event的推荐度\n",
    "    基本的伪代码思路如下：\n",
    "    for item i\n",
    "      for every other user v that has a preference for i\n",
    "        compute similarity s between u and v\n",
    "        incorporate v's preference for i weighted by s into running aversge\n",
    "    return top items ranked by weighted average\n",
    "    \"\"\"\n",
    "    i = self.userIndex[userId]\n",
    "    j = self.eventIndex[eventId]\n",
    "\n",
    "    vs = self.userEventScores[:, j]\n",
    "    sims = self.userSimMatrix[i, :]\n",
    "\n",
    "    prod = sims * vs\n",
    "\n",
    "    try:\n",
    "      return prod[0, 0] - self.userEventScores[i, j]\n",
    "    except IndexError:\n",
    "      return 0\n",
    "\n",
    "  def eventReco(self, userId, eventId):\n",
    "    \"\"\"\n",
    "    类似基于Item-based协同过滤，只是item之间的相似度由item本身的属性得到，计算Event的推荐度\n",
    "    基本的伪代码思路如下：\n",
    "    for item i \n",
    "      for every item j that u has a preference for\n",
    "        compute similarity s between i and j\n",
    "        add u's preference for j weighted by s to a running average\n",
    "    return top items, ranked by weighted average\n",
    "    \"\"\"\n",
    "    i = self.userIndex[userId]\n",
    "    j = self.eventIndex[eventId]\n",
    "    js = self.userEventScores[i, :]\n",
    "    psim = self.eventPropSim[:, j]\n",
    "    csim = self.eventContSim[:, j]\n",
    "    pprod = js * psim\n",
    "    cprod = js * csim\n",
    "    \n",
    "    pscore = 0\n",
    "    cscore = 0\n",
    "    try:\n",
    "      pscore = pprod[0, 0] - self.userEventScores[i, j]\n",
    "    except IndexError:\n",
    "      pass\n",
    "    try:\n",
    "      cscore = cprod[0, 0] - self.userEventScores[i, j]\n",
    "    except IndexError:\n",
    "      pass\n",
    "    return pscore, cscore\n",
    "\n",
    "  def userPop(self, userId):\n",
    "    \"\"\"\n",
    "    基于用户的朋友个数来推断用户的社交程度\n",
    "    主要的考量是如果用户的朋友非常多，可能会更倾向于参加各种社交活动\n",
    "    \"\"\"\n",
    "    if userId in self.userIndex:\n",
    "      i = self.userIndex[userId]\n",
    "      try:\n",
    "        return self.numFriends[0, i]\n",
    "      except IndexError:\n",
    "        return 0\n",
    "    else:\n",
    "      return 0\n",
    "\n",
    "  def friendInfluence(self, userId):\n",
    "    \"\"\"\n",
    "    朋友对用户的影响\n",
    "    主要考虑用户所有的朋友中，有多少是非常喜欢参加各种社交活动/event的\n",
    "    用户的朋友圈如果都积极参与各种event，可能会对当前用户有一定的影响\n",
    "    \"\"\"\n",
    "    nusers = np.shape(self.userFriends)[1]\n",
    "    i = self.userIndex[userId]\n",
    "    return (self.userFriends[i, :].sum(axis=0) / nusers)[0,0]\n",
    "\n",
    "  def eventPop(self, eventId):\n",
    "    \"\"\"\n",
    "    本活动本身的热度\n",
    "    主要是通过参与的人数来界定的\n",
    "    \"\"\"\n",
    "    i = self.eventIndex[eventId]\n",
    "    return self.eventPopularity[i, 0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def generateRSData(RS, train=True, header=True):\n",
    "    \"\"\"\n",
    "    把前面user-based协同过滤 和 item-based协同过滤，以及各种热度和影响度作为特征组合在一起\n",
    "    生成新的训练数据，用于分类器分类使用\n",
    "    \"\"\"\n",
    "    fn = \"train.csv\" if train else \"test.csv\"\n",
    "    fin = open(dataPath+fn, 'rb')\n",
    "    fout = open(dataPath+\"RS_\" + fn, 'w')\n",
    "    \n",
    "    #忽略第一行（列名字）\n",
    "    fin.readline().strip().decode().split(\",\")\n",
    "    \n",
    "    # write output header\n",
    "    if header:\n",
    "      ocolnames = [\"invited\", \"userCF_reco\", \"evtCF_reco\",\"svdCF_reco\",\"user_reco\", \"evt_p_reco\",\n",
    "        \"evt_c_reco\", \"user_pop\", \"frnd_infl\", \"evt_pop\"]\n",
    "      if train:\n",
    "        ocolnames.append(\"interested\")\n",
    "        ocolnames.append(\"not_interested\")\n",
    "      fout.write(\",\".join(ocolnames) + \"\\n\")\n",
    "    \n",
    "    ln = 0\n",
    "    for line in fin:\n",
    "      ln += 1\n",
    "      if ln%500 == 0:\n",
    "          print(\"%s:%d (userId, eventId)=(%s, %s)\" % (fn, ln, userId, eventId)) \n",
    "          #break;\n",
    "      \n",
    "      cols = line.strip().decode().split(\",\")\n",
    "      userId = cols[0]\n",
    "      eventId = cols[1]\n",
    "      invited = cols[2]\n",
    "      \n",
    "      userCF_reco = RS.userCFReco(userId, eventId)\n",
    "      itemCF_reco = RS.eventCFReco(userId, eventId)\n",
    "      svdCF_reco = RS.svdCFReco(userId, eventId)\n",
    "        \n",
    "      user_reco = RS.userReco(userId, eventId)\n",
    "      evt_p_reco, evt_c_reco = RS.eventReco(userId, eventId)\n",
    "      user_pop = RS.userPop(userId)\n",
    "     \n",
    "      frnd_infl = RS.friendInfluence(userId)\n",
    "      evt_pop = RS.eventPop(eventId)\n",
    "      ocols = [invited, userCF_reco, itemCF_reco, svdCF_reco,user_reco, evt_p_reco,\n",
    "        evt_c_reco, user_pop, frnd_infl, evt_pop]\n",
    "      \n",
    "      if train:\n",
    "        ocols.append(cols[4]) # interested\n",
    "        ocols.append(cols[5]) # not_interested\n",
    "      fout.write(\",\".join(map(lambda x: str(x), ocols)) + \"\\n\")\n",
    "    \n",
    "    fin.close()\n",
    "    fout.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVD Train...\n",
      "已走0步,用了0条训练数据\n",
      "Puk= [0.22407013 0.27289802 0.23901356 0.29612146 0.19134008 0.25360524\n",
      " 0.25706745 0.37894878 0.08832739 0.13715131 0.18520683 0.24639224\n",
      " 0.11107953 0.21861173 0.30410067 0.36703148 0.18109227 0.20847592\n",
      " 0.27790115 0.19947083]\n",
      "Qki= [0.24220081 0.1045278  0.30465465 0.3093809  0.43454403 0.26501912\n",
      " 0.37941791 0.20988745 0.25626011 0.30966513 0.15598168 0.3886366\n",
      " 0.2760765  0.33200304 0.29070889 0.06822532 0.1348396  0.297768\n",
      " 0.30318689 0.3397577 ]\n",
      "Bu= -0.04\n",
      "Bi= -0.04\n",
      "已走0步,用了5000条训练数据\n",
      "Puk= [0.22303581 0.31946221 0.11062972 0.13437601 0.26542573 0.26742212\n",
      " 0.3067187  0.03850987 0.32783971 0.38124815 0.39652625 0.05423152\n",
      " 0.04016998 0.22949662 0.22855487 0.01245213 0.04635644 0.09716653\n",
      " 0.34210278 0.2945485 ]\n",
      "Qki= [0.04298701 0.21204505 0.41513239 0.23491847 0.1685097  0.15550692\n",
      " 0.37764848 0.34378489 0.14149646 0.39540075 0.33099452 0.36828838\n",
      " 0.23968007 0.11125166 0.17703913 0.2389221  0.05933893 0.36314724\n",
      " 0.09182683 0.37525922]\n",
      "Bu= -0.07952144\n",
      "Bi= -0.04\n",
      "已走0步,用了10000条训练数据\n",
      "Puk= [ 0.18701435  0.02485341 -0.0513455   0.26680498  0.2463588   0.02500425\n",
      " -0.10567972  0.21973631  0.19415126 -0.07100012  0.15370813 -0.09564192\n",
      "  0.10450845  0.21195061  0.0533805   0.25218752 -0.02614937  0.02001296\n",
      "  0.25338091  0.00501983]\n",
      "Qki= [0.03499153 0.07672301 0.32024427 0.11064381 0.24223128 0.03229905\n",
      " 0.00132343 0.15963272 0.20175769 0.23918762 0.19357809 0.08799179\n",
      " 0.10178602 0.09938162 0.24459317 0.43736954 0.33132419 0.23623596\n",
      " 0.31124121 0.18718549]\n",
      "Bu= -0.4040080672134896\n",
      "Bi= 0.03284423186694478\n",
      "已走0步,用了15000条训练数据\n",
      "Puk= [ 0.37351176 -0.00848378  0.01983665  0.35202497  0.16683341  0.41012845\n",
      "  0.24141497  0.16871522  0.27984045  0.16396789  0.27807003  0.23812596\n",
      "  0.22795719  0.29713845  0.03143744  0.00814612  0.03229535  0.14839143\n",
      "  0.07432089  0.27981634]\n",
      "Qki= [ 0.21558834  0.37705672  0.2180995   0.05124961  0.17255386  0.00311977\n",
      "  0.18024582  0.25558086  0.08243849  0.25178281  0.42034546  0.16259825\n",
      "  0.40651507 -0.0090967   0.17339557  0.34194127  0.0064685   0.25573704\n",
      "  0.13284203  0.23307353]\n",
      "Bu= -0.04\n",
      "Bi= -0.04\n",
      "已走10步,用了0条训练数据\n",
      "Puk= [ 0.10482709  0.09396834  0.08247888  0.1410317   0.02255737  0.12565291\n",
      "  0.10641488  0.14687201  0.01550768  0.01474254  0.08977507  0.10421477\n",
      " -0.01194739  0.07963266  0.13601407  0.22757073  0.05334768  0.04773642\n",
      "  0.11855349  0.08819886]\n",
      "Qki= [0.19260743 0.05986956 0.24369958 0.23743042 0.37098203 0.20899811\n",
      " 0.30187178 0.15040693 0.18950749 0.2689811  0.11836263 0.32245588\n",
      " 0.21266133 0.25555005 0.22210786 0.00684757 0.10458964 0.22145731\n",
      " 0.22690142 0.2749391 ]\n",
      "Bu= -0.2474543791127647\n",
      "Bi= -0.2688640231931969\n",
      "已走10步,用了5000条训练数据\n",
      "Puk= [ 0.11988057  0.12933502  0.05790484 -0.01349442  0.14322197  0.07431721\n",
      "  0.07878354 -0.00289755  0.13365223  0.08895273  0.11048502 -0.04288585\n",
      "  0.05152575  0.1031967   0.14780771 -0.09540252  0.02596419  0.01290176\n",
      "  0.12455866  0.1511467 ]\n",
      "Qki= [0.0165963  0.15717536 0.35855485 0.20179458 0.12160704 0.11525404\n",
      " 0.31054447 0.3078957  0.09200354 0.31624963 0.25718786 0.33175103\n",
      " 0.20796002 0.07412905 0.13139801 0.22106761 0.05118816 0.31672631\n",
      " 0.04642824 0.30217809]\n",
      "Bu= -0.21840364795687925\n",
      "Bi= -0.18172449191256304\n",
      "已走10步,用了10000条训练数据\n",
      "Puk= [-0.01152404  0.02021857  0.01545417 -0.01011443  0.02959469 -0.06728285\n",
      " -0.03460192  0.02160636 -0.00282759 -0.00841579 -0.03486765 -0.02502245\n",
      "  0.00612889 -0.01115255 -0.02473017  0.02761885  0.02347433 -0.01326008\n",
      "  0.08072758 -0.00875379]\n",
      "Qki= [ 0.03623807  0.07731844  0.30156938  0.10898476  0.24101132  0.01500898\n",
      " -0.01082222  0.1608495   0.19396462  0.21974048  0.17821688  0.07301225\n",
      "  0.10086351  0.09728011  0.22438334  0.42447029  0.31427894  0.21842332\n",
      "  0.31854094  0.17289111]\n",
      "Bu= -0.05890848448733968\n",
      "Bi= 0.28503968197031637\n",
      "已走10步,用了15000条训练数据\n",
      "Puk= [ 0.22497334 -0.06804591 -0.00136472  0.17616511  0.08550862  0.26481353\n",
      "  0.10028442  0.03141993  0.1791739   0.04243079  0.12643452  0.14641596\n",
      "  0.0975769   0.24721328  0.03313493 -0.00731974 -0.01627624  0.10151773\n",
      "  0.04968267  0.20343557]\n",
      "Qki= [ 0.16807843  0.36076853  0.20544981  0.01795109  0.14927703 -0.03632246\n",
      "  0.15065409  0.23065351  0.05093001  0.22672465  0.37353941  0.13038769\n",
      "  0.36397018 -0.04019347  0.16058739  0.32318016  0.0067267   0.22742167\n",
      "  0.11791934  0.19186762]\n",
      "Bu= -0.17620359223196913\n",
      "Bi= -0.15876947367737018\n",
      "已走20步,用了0条训练数据\n",
      "Puk= [ 0.06193039  0.04208763  0.05107961  0.08329972  0.00493252  0.08761543\n",
      "  0.06196875  0.07419444  0.00803871  0.0035446   0.058109    0.06570627\n",
      " -0.02065117  0.03390971  0.08760051  0.15820298  0.02309886  0.01083952\n",
      "  0.0711511   0.0667467 ]\n",
      "Qki= [0.16952801 0.05165975 0.21474379 0.20837442 0.32865016 0.18359537\n",
      " 0.26608767 0.13141117 0.16605567 0.23871422 0.10374094 0.28492056\n",
      " 0.18738972 0.22441744 0.19454522 0.00155794 0.09216289 0.1945209\n",
      " 0.19835483 0.24232134]\n",
      "Bu= -0.14235592898435234\n",
      "Bi= -0.26640368312143287\n",
      "已走20步,用了5000条训练数据\n",
      "Puk= [ 0.09121765  0.08275302  0.05601186 -0.02617348  0.10462423  0.03671287\n",
      "  0.04311102  0.01667791  0.08047515  0.02188755  0.04442665 -0.02215891\n",
      "  0.05596475  0.06177133  0.11972353 -0.08405989  0.03244838  0.00799709\n",
      "  0.05943898  0.11263519]\n",
      "Qki= [0.00921894 0.13389595 0.31483858 0.18005415 0.10138082 0.09939995\n",
      " 0.27224592 0.27254782 0.0760748  0.2777022  0.22417631 0.29582486\n",
      " 0.18145424 0.0614701  0.10945204 0.20076288 0.04384346 0.28026792\n",
      " 0.03654127 0.26103024]\n",
      "Bu= -0.11222262639723644\n",
      "Bi= -0.21360790388517742\n",
      "已走20步,用了10000条训练数据\n",
      "Puk= [-0.01289294  0.01759268  0.00869129 -0.0061805   0.02316436 -0.05417116\n",
      " -0.0244851   0.0191857  -0.00235552 -0.01089073 -0.03095088 -0.02140118\n",
      "  0.00103857 -0.01052428 -0.01857441  0.01713997  0.01922467 -0.011691\n",
      "  0.06183508 -0.00542581]\n",
      "Qki= [ 0.0319506   0.07569576  0.28506343  0.10099925  0.23060003  0.0040659\n",
      " -0.01496491  0.15441834  0.18168581  0.20467981  0.1618212   0.06474168\n",
      "  0.09517202  0.08949515  0.20706674  0.4020723   0.29859782  0.20297291\n",
      "  0.31074145  0.16117593]\n",
      "Bu= -0.04500691581140384\n",
      "Bi= 0.4332291752704299\n",
      "已走20步,用了15000条训练数据\n",
      "Puk= [ 0.1693907  -0.06990988  0.00844957  0.11650185  0.07068116  0.20891386\n",
      "  0.06102197  0.00202476  0.14323152  0.02536893  0.09478647  0.10729897\n",
      "  0.05848411  0.22229688  0.04528797  0.01102136 -0.0189859   0.09823899\n",
      "  0.04201673  0.18110869]\n",
      "Qki= [ 0.14647887  0.34376648  0.19309607  0.00832042  0.1357837  -0.04832768\n",
      "  0.13711297  0.21623712  0.03830938  0.21145663  0.34502803  0.11518558\n",
      "  0.33805072 -0.0520647   0.14864253  0.30398489  0.00739513  0.20794934\n",
      "  0.10822761  0.16894874]\n",
      "Bu= -0.07719587950000933\n",
      "Bi= -0.20970631931894426\n",
      "已走30步,用了0条训练数据\n",
      "Puk= [ 0.03356194  0.0084523   0.03044094  0.04677132  0.00011647  0.06129667\n",
      "  0.03397926  0.02947274  0.00357641  0.00278709  0.03552037  0.04068258\n",
      " -0.02122211  0.00430972  0.05759402  0.10931098  0.00368027 -0.0084824\n",
      "  0.04133506  0.05332795]\n",
      "Qki= [1.50023993e-01 4.56640110e-02 1.90124435e-01 1.84302647e-01\n",
      " 2.91380738e-01 1.62300348e-01 2.35621109e-01 1.16184943e-01\n",
      " 1.46970030e-01 2.11699537e-01 9.17214012e-02 2.52364375e-01\n",
      " 1.66140607e-01 1.98689380e-01 1.71899220e-01 2.12855886e-04\n",
      " 8.16791694e-02 1.72300098e-01 1.75279327e-01 2.14428236e-01]\n",
      "Bu= -0.0856825353563976\n",
      "Bi= -0.2463413215001753\n",
      "已走30步,用了5000条训练数据\n",
      "Puk= [ 0.06943103  0.05711614  0.04876325 -0.02580165  0.0785238   0.01998632\n",
      "  0.02689786  0.02198744  0.0526461  -0.00265941  0.01737051 -0.01051532\n",
      "  0.04950127  0.03959326  0.09448776 -0.06824404  0.03111699  0.00586801\n",
      "  0.03037643  0.08597242]\n",
      "Qki= [0.00510304 0.1160498  0.27708928 0.16066981 0.0863767  0.08707919\n",
      " 0.24006082 0.24087718 0.06491524 0.24590852 0.19763008 0.26290374\n",
      " 0.15881298 0.05257003 0.09290049 0.18095378 0.03765045 0.24822251\n",
      " 0.03072924 0.22760641]\n",
      "Bu= -0.06268123480718728\n",
      "Bi= -0.22825357640447927\n",
      "已走30步,用了10000条训练数据\n",
      "Puk= [-1.29758522e-02  1.60019929e-02  4.37148558e-03 -4.14348869e-03\n",
      "  1.78452131e-02 -4.27129687e-02 -1.77518068e-02  1.51723008e-02\n",
      " -2.99742132e-03 -1.26158800e-02 -2.61252872e-02 -1.90481618e-02\n",
      " -4.89092237e-05 -8.99602015e-03 -1.26340765e-02  1.12609441e-02\n",
      "  1.46931697e-02 -9.44243896e-03  4.73619770e-02 -4.01746538e-03]\n",
      "Qki= [ 0.02855301  0.07308774  0.26875133  0.09439523  0.21914726 -0.00166963\n",
      " -0.01644452  0.14715478  0.1705294   0.19109255  0.14890227  0.05857002\n",
      "  0.08952047  0.08303413  0.19293906  0.3796361   0.28268631  0.18964541\n",
      "  0.29835142  0.15102619]\n",
      "Bu= -0.03501481698116705\n",
      "Bi= 0.5220405662382812\n",
      "已走30步,用了15000条训练数据\n",
      "Puk= [ 0.12961318 -0.06939669  0.01276283  0.07861156  0.05459749  0.16605703\n",
      "  0.03736374 -0.01489381  0.11596678  0.01750378  0.07339233  0.07787097\n",
      "  0.03553526  0.19109444  0.04471576  0.02237474 -0.02314296  0.0882677\n",
      "  0.03501514  0.15481796]\n",
      "Qki= [ 0.13056803  0.32708169  0.18121962  0.00310824  0.12473553 -0.05470889\n",
      "  0.12671134  0.2039472   0.02968479  0.1980251   0.32067363  0.10390771\n",
      "  0.31597301 -0.05925516  0.13765544  0.28527878  0.00801846  0.19111514\n",
      "  0.09997832  0.1507201 ]\n",
      "Bu= -0.006063409219651007\n",
      "Bi= -0.24703207150977632\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已走40步,用了0条训练数据\n",
      "Puk= [ 0.01458465 -0.01439945  0.01706323  0.02209921 -0.00089114  0.04230636\n",
      "  0.01494018  0.00068149  0.00037434  0.00510789  0.01926231  0.02334667\n",
      " -0.01975611 -0.01647869  0.0385015   0.07570522 -0.00913229 -0.01955269\n",
      "  0.02088321  0.04378134]\n",
      "Qki= [ 0.13308104  0.04069935  0.16861328  0.163434    0.2584156   0.14388161\n",
      "  0.20899532  0.10313745  0.13010375  0.18781226  0.08148116  0.22393658\n",
      "  0.14729171  0.17608691  0.15218533 -0.00042055  0.07262209  0.1526657\n",
      "  0.15510018  0.19001418]\n",
      "Bu= -0.05719181086183925\n",
      "Bi= -0.22827500341896306\n",
      "已走40步,用了5000条训练数据\n",
      "Puk= [ 0.05488075  0.04237051  0.0416491  -0.02227914  0.0611883   0.01246576\n",
      "  0.01929772  0.02279192  0.03708115 -0.01075633  0.00613241 -0.00395521\n",
      "  0.0421327   0.02752909  0.07550943 -0.05426952  0.02804776  0.00529608\n",
      "  0.01660647  0.06761915]\n",
      "Qki= [0.00255543 0.10131597 0.24421134 0.14322944 0.0743632  0.07670524\n",
      " 0.21211515 0.21284033 0.05613484 0.21826137 0.17486962 0.23331971\n",
      " 0.13932867 0.04554669 0.07964922 0.16239601 0.0324417  0.21989645\n",
      " 0.02652045 0.19934622]\n",
      "Bu= -0.03945512521835147\n",
      "Bi= -0.23458994691757504\n",
      "已走40步,用了10000条训练数据\n",
      "Puk= [-0.01274587  0.01509828  0.00175722 -0.00314567  0.01406685 -0.03438608\n",
      " -0.01345976  0.01161443 -0.00379658 -0.01357028 -0.02222785 -0.01740425\n",
      "  0.00010366 -0.00764816 -0.0081902   0.0078409   0.01116706 -0.00763773\n",
      "  0.03750287 -0.00360723]\n",
      "Qki= [ 0.02578348  0.07006858  0.25310537  0.0885158   0.20751366 -0.00476873\n",
      " -0.01675959  0.13956626  0.16016064  0.17868745  0.13808543  0.05358342\n",
      "  0.08422722  0.07743177  0.1806812   0.35797506  0.26704594  0.17772554\n",
      "  0.28422742  0.14178252]\n",
      "Bu= -0.027955356646641952\n",
      "Bi= 0.5751441070421954\n",
      "已走40步,用了15000条训练数据\n",
      "Puk= [ 0.10084999 -0.06780706  0.01500298  0.0523617   0.04101     0.13268857\n",
      "  0.02199284 -0.02467492  0.09463314  0.01388492  0.05831114  0.05568618\n",
      "  0.02122306  0.16172888  0.04081787  0.02988954 -0.02712108  0.07836575\n",
      "  0.02920174  0.1309351 ]\n",
      "Qki= [ 1.17995428e-01  3.10900434e-01  1.69988254e-01  1.61464149e-04\n",
      "  1.15385208e-01 -5.79049859e-02  1.18048773e-01  1.92896989e-01\n",
      "  2.34254752e-02  1.85757373e-01  2.99067989e-01  9.49812077e-02\n",
      "  2.96275992e-01 -6.33866845e-02  1.27732741e-01  2.67400807e-01\n",
      "  8.65199317e-03  1.76317201e-01  9.27393251e-02  1.35734298e-01]\n",
      "Bu= 0.04299518507891614\n",
      "Bi= -0.2758451997952849\n",
      "已走50步,用了0条训练数据\n",
      "Puk= [ 0.00196951 -0.03069338  0.00886603  0.00485931 -0.0007585   0.02847632\n",
      "  0.00152558 -0.01838047 -0.00208823  0.00840626  0.00744287  0.01108591\n",
      " -0.01801459 -0.03167899  0.02664815  0.05292591 -0.01784089 -0.02608027\n",
      "  0.0061374   0.03665318]\n",
      "Qki= [ 0.11835019  0.03664658  0.14985505  0.14536111  0.22932077  0.12797469\n",
      "  0.1857496   0.09194272  0.11522782  0.16674112  0.07288228  0.1992033\n",
      "  0.13067628  0.15621325  0.13495052 -0.0007145   0.06487911  0.13525819\n",
      "  0.13737951  0.16864326]\n",
      "Bu= -0.043225125293411035\n",
      "Bi= -0.21519576884965308\n",
      "已走50步,用了5000条训练数据\n",
      "Puk= [ 0.04512476  0.03345783  0.0357489  -0.01844053  0.04941844  0.00909546\n",
      "  0.01559769  0.02203568  0.02798456 -0.01236552  0.0017037  -0.00018001\n",
      "  0.03590967  0.02073274  0.06179679 -0.04322382  0.02483977  0.0052924\n",
      "  0.00986254  0.05484099]\n",
      "Qki= [0.00087071 0.08876873 0.21542384 0.12756415 0.0643843  0.06771299\n",
      " 0.18757753 0.18807216 0.04886133 0.193844   0.15494029 0.20691901\n",
      " 0.12242697 0.03970745 0.06868827 0.14535079 0.02802513 0.19481067\n",
      " 0.02315341 0.17502199]\n",
      "Bu= -0.028868840679124916\n",
      "Bi= -0.23631993480069785\n",
      "已走50步,用了10000条训练数据\n",
      "Puk= [-0.01246768  0.01453655  0.00020758 -0.00264589  0.01156379 -0.02868664\n",
      " -0.0107253   0.00888373 -0.00440925 -0.01386712 -0.01931194 -0.01613205\n",
      "  0.00047569 -0.00666408 -0.00515062  0.005832    0.00866932 -0.00629255\n",
      "  0.0310129  -0.00351963]\n",
      "Qki= [ 0.02344001  0.06691586  0.23827025  0.08312108  0.19613323 -0.00653104\n",
      " -0.01655334  0.13201614  0.1504653   0.16727082  0.12861027  0.0493355\n",
      "  0.07929162  0.07241108  0.16962684  0.33735137  0.25197315  0.16681733\n",
      "  0.26972131  0.13320798]\n",
      "Bu= -0.023342310976259883\n",
      "Bi= 0.6067765411170725\n",
      "已走50步,用了15000条训练数据\n",
      "Puk= [ 0.08031896 -0.06557191  0.01633742  0.03357709  0.030547    0.10690868\n",
      "  0.01179223 -0.02989053  0.07793616  0.01262783  0.04775252  0.0392779\n",
      "  0.01222535  0.13638381  0.03666271  0.03495999 -0.03028482  0.07027226\n",
      "  0.02461683  0.11125133]\n",
      "Qki= [ 0.10763381  0.2952785   0.15942278 -0.0014608   0.10727001 -0.05909105\n",
      "  0.11051295  0.18268045  0.01875174  0.17437433  0.27953127  0.08762442\n",
      "  0.27830873 -0.06539464  0.11876016  0.25047088  0.00926655  0.16313273\n",
      "  0.08627776  0.12314105]\n",
      "Bu= 0.07555499823219024\n",
      "Bi= -0.29827061698711965\n",
      "已走60步,用了0条训练数据\n",
      "Puk= [-0.00632717 -0.04305712  0.00424841 -0.00752594 -0.00041565  0.01834314\n",
      " -0.00821765 -0.03138502 -0.00408624  0.01195629 -0.00129137  0.00228152\n",
      " -0.01660526 -0.04314385  0.01969527  0.03764291 -0.02410685 -0.0299397\n",
      " -0.00494167  0.03115474]\n",
      "Qki= [ 0.10552766  0.03339175  0.13352407  0.1297532   0.20369215  0.11424585\n",
      "  0.16547031  0.08234452  0.10221028  0.14815568  0.06574216  0.17772179\n",
      "  0.11616655  0.13883031  0.11990343 -0.00073126  0.05831231  0.11986023\n",
      "  0.12189461  0.14992609]\n",
      "Bu= -0.03686102766051558\n",
      "Bi= -0.20654203848330766\n",
      "已走60步,用了5000条训练数据\n",
      "Puk= [ 0.03834547  0.02777598  0.03108685 -0.01508846  0.04118006  0.00758741\n",
      "  0.01368165  0.02078183  0.02244198 -0.01157654  0.00021337  0.00205159\n",
      "  0.03101161  0.01670515  0.05190127 -0.03483393  0.02196457  0.00541518\n",
      "  0.00647244  0.04578188]\n",
      "Qki= [-0.00029381  0.07791948  0.19013195  0.11353142  0.05592429  0.05982469\n",
      "  0.1659331   0.16619401  0.0426754   0.17217214  0.13735018  0.18343078\n",
      "  0.10768079  0.0347247   0.0594386   0.12987027  0.02425051  0.17258051\n",
      "  0.02032416  0.15388299]\n",
      "Bu= -0.02452153515227449\n",
      "Bi= -0.23541577399506902\n",
      "已走60步,用了10000条训练数据\n",
      "Puk= [-0.01216067  0.01404172 -0.00081159 -0.00235066  0.00987877 -0.02479608\n",
      " -0.00898398  0.00692596 -0.00484148 -0.01382453 -0.01716524 -0.01518771\n",
      "  0.00077295 -0.00604739 -0.00308065  0.00451539  0.00697247 -0.0053645\n",
      "  0.02665039 -0.00350606]\n",
      "Qki= [ 0.02139289  0.06376605  0.22426263  0.0781066   0.18519787 -0.00759093\n",
      " -0.01611173  0.12469004  0.14137868  0.15669528  0.12007348  0.0455886\n",
      "  0.07467231  0.06781655  0.15945317  0.3178251   0.23760154  0.15671062\n",
      "  0.25544347  0.12519678]\n",
      "Bu= -0.02040605223875374\n",
      "Bi= 0.6255714958758324\n",
      "已走60步,用了15000条训练数据\n",
      "Puk= [ 0.06593764 -0.06298328  0.01722255  0.0199112   0.02277611  0.08708284\n",
      "  0.00499553 -0.0321554   0.06489445  0.01275129  0.04049624  0.02736292\n",
      "  0.00670276  0.11515376  0.03311818  0.03844694 -0.0326208   0.06413706\n",
      "  0.02114278  0.0957601 ]\n",
      "Qki= [ 0.09882483  0.28023289  0.14950525 -0.0022728   0.1000823  -0.05896516\n",
      "  0.10377145  0.17308009  0.01519634  0.16372943  0.26165361  0.08136562\n",
      "  0.2617075  -0.0659039   0.11060172  0.23452348  0.00982604  0.15125875\n",
      "  0.08043958  0.11236402]\n",
      "Bu= 0.096486841279704\n",
      "Bi= -0.31548567655655185\n",
      "已走70步,用了0条训练数据\n",
      "Puk= [-0.01170462 -0.0530309   0.00207423 -0.01666577 -0.00015935  0.0108994\n",
      " -0.01550769 -0.04052032 -0.00577219  0.01547989 -0.00787599 -0.00413193\n",
      " -0.01567383 -0.05203608  0.01609583  0.02751998 -0.02892979 -0.03214457\n",
      " -0.01355802  0.02680134]\n",
      "Qki= [ 0.09433544  0.03080027  0.11929448  0.11627937  0.18113792  0.10236533\n",
      "  0.14776815  0.07409284  0.09085722  0.13175272  0.05983867  0.15906862\n",
      "  0.10356405  0.12367996  0.10676796 -0.00052996  0.05276183  0.10625338\n",
      "  0.10840745  0.13349698]\n",
      "Bu= -0.03446064772264136\n",
      "Bi= -0.2011280474299237\n",
      "已走70步,用了5000条训练数据\n",
      "Puk= [ 3.34500432e-02  2.39640494e-02  2.74587468e-02 -1.23687319e-02\n",
      "  3.52460545e-02  6.90646146e-03  1.26013433e-02  1.94584806e-02\n",
      "  1.89136780e-02 -1.00682286e-02 -4.93637359e-05  3.41398098e-03\n",
      "  2.72195227e-02  1.41718486e-02  4.46704794e-02 -2.85398248e-02\n",
      "  1.95410695e-02  5.52043093e-03  4.72290093e-03  3.92329433e-02]\n",
      "Qki= [-0.0011231   0.06846052  0.16786077  0.10098993  0.04866284  0.0528682\n",
      "  0.14680034  0.14686154  0.03733754  0.15290938  0.12177475  0.1625645\n",
      "  0.09476409  0.03041381  0.05153167  0.11590781  0.02100327  0.15287679\n",
      "  0.01788758  0.13540633]\n",
      "Bu= -0.023337461155998412\n",
      "Bi= -0.23305453097495216\n",
      "已走70步,用了10000条训练数据\n",
      "Puk= [-0.01185893  0.01354821 -0.0015267  -0.00214459  0.00870295 -0.02210553\n",
      " -0.00785767  0.0055632  -0.0051614  -0.01360351 -0.01557691 -0.01449184\n",
      "  0.0009384  -0.00571239 -0.00167282  0.00354938  0.00580686 -0.00475058\n",
      "  0.02358375 -0.00348161]\n",
      "Qki= [ 0.01956286  0.06068814  0.21105435  0.07341967  0.17478049 -0.00826152\n",
      " -0.01556676  0.11767354  0.13284782  0.14684925  0.11225262  0.04220564\n",
      "  0.07033585  0.0635596   0.14999484  0.2993798   0.22397215  0.14728153\n",
      "  0.24166019  0.11768814]\n",
      "Bu= -0.018587449203450016\n",
      "Bi= 0.6367460768995294\n",
      "已走70步,用了15000条训练数据\n",
      "Puk= [ 0.05609366 -0.0603325   0.01781247  0.00982518  0.01704083  0.07182359\n",
      "  0.00044358 -0.03257154  0.05468754  0.01363955  0.03557651  0.01882862\n",
      "  0.00343686  0.0974847   0.03029492  0.04083677 -0.03432435  0.05963826\n",
      "  0.01857658  0.08385258]\n",
      "Qki= [ 0.09114163  0.26578024  0.14020389 -0.00258639  0.09361154 -0.05799058\n",
      "  0.09762594  0.16398006  0.01244141  0.15373394  0.24515718  0.07589649\n",
      "  0.24624579 -0.06536982  0.10313602  0.21954526  0.01031176  0.14046609\n",
      "  0.07511244  0.10298123]\n",
      "Bu= 0.10971600379623178\n",
      "Bi= -0.32844048893424926\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已走80步,用了0条训练数据\n",
      "Puk= [-1.51232466e-02 -6.14725336e-02  1.55678752e-03 -2.35898210e-02\n",
      " -5.56826302e-05  5.43110083e-03 -2.11256950e-02 -4.70975066e-02\n",
      " -7.22816482e-03  1.88513026e-02 -1.29469009e-02 -8.87367873e-03\n",
      " -1.51780442e-02 -5.91040464e-02  1.48006569e-02  2.09386508e-02\n",
      " -3.28849920e-02 -3.32675613e-02 -2.04448085e-02  2.32768670e-02]\n",
      "Qki= [ 0.08453417  0.02874796  0.10686775  0.10463401  0.16129084  0.09203608\n",
      "  0.13229834  0.06696684  0.08095136  0.11726372  0.05495633  0.14286335\n",
      "  0.09264012  0.11049949  0.09528432 -0.00017731  0.04807326  0.09422787\n",
      "  0.09667075  0.11903283]\n",
      "Bu= -0.0341053513204756\n",
      "Bi= -0.1979103712666712\n",
      "已走80步,用了5000条训练数据\n",
      "Puk= [ 0.0297963   0.02128774  0.02464033 -0.01021593  0.03086111  0.00659157\n",
      "  0.01193435  0.01823908  0.01656183 -0.00849251  0.00016432  0.00428256\n",
      "  0.02427613  0.01247775  0.03928767 -0.02380247  0.01755795  0.00558454\n",
      "  0.00379231  0.03440185]\n",
      "Qki= [-0.00172523  0.06017389  0.14821915  0.08980034  0.04238091  0.04671828\n",
      "  0.12986974  0.12977087  0.03269256  0.13578385  0.10796508  0.14404143\n",
      "  0.08341918  0.02665542  0.04471459  0.10336999  0.01819511  0.13541104\n",
      "  0.0157625   0.11919804]\n",
      "Bu= -0.023779175493227583\n",
      "Bi= -0.22996321377533965\n",
      "已走80步,用了10000条训练数据\n",
      "Puk= [-0.0115835   0.0130472  -0.00205177 -0.00199266  0.00785016 -0.02019871\n",
      " -0.00711151  0.00461269 -0.00540364 -0.01329089 -0.01437083 -0.0139628\n",
      "  0.00099363 -0.00556981 -0.00070954  0.00277452  0.00498711 -0.00434325\n",
      "  0.0213213  -0.00343235]\n",
      "Qki= [ 0.01789997  0.05771758  0.19860605  0.06902653  0.16490091 -0.00870437\n",
      " -0.01498214  0.11099917  0.12482915  0.13765103  0.10501768  0.03910623\n",
      "  0.06625729  0.05958699  0.14115608  0.28197366  0.21108289  0.13845177\n",
      "  0.22848261  0.11063917]\n",
      "Bu= -0.017499758557985023\n",
      "Bi= 0.6434237929180147\n",
      "已走80步,用了15000条训练数据\n",
      "Puk= [ 0.04953762 -0.0578484   0.01816313  0.00225166  0.01275682  0.06001076\n",
      " -0.00264392 -0.03189893  0.04663958  0.01488032  0.03224089  0.01277138\n",
      "  0.00158507  0.08272404  0.02808925  0.04240903 -0.0356095   0.05636943\n",
      "  0.01672267  0.07479475]\n",
      "Qki= [ 0.08429987  0.25193915  0.13148256 -0.00259413  0.08771449 -0.0564807\n",
      "  0.09195481  0.15532185  0.01026486  0.14432878  0.22984575  0.0710141\n",
      "  0.23177578 -0.06412428  0.09626427  0.20549726  0.01072199  0.13058072\n",
      "  0.0702131   0.09468356]\n",
      "Bu= 0.11797410040667103\n",
      "Bi= -0.3379922941407929\n",
      "已走90步,用了0条训练数据\n",
      "Puk= [-1.72389745e-02 -6.88350502e-02  2.14506339e-03 -2.89601220e-02\n",
      " -9.88138956e-05  1.42830302e-03 -2.55824012e-02 -5.19086179e-02\n",
      " -8.48714900e-03  2.19911784e-02 -1.69383185e-02 -1.24385192e-02\n",
      " -1.50132371e-02 -6.48389244e-02  1.50700490e-02  1.67896925e-02\n",
      " -3.62914590e-02 -3.36557322e-02 -2.60588058e-02  2.03729872e-02]\n",
      "Qki= [0.07592299 0.02712922 0.09598239 0.09454746 0.14381617 0.08300372\n",
      " 0.11876128 0.06078176 0.07228545 0.10445207 0.05090335 0.12876967\n",
      " 0.08316822 0.09904196 0.08521803 0.0002669  0.04410735 0.08359426\n",
      " 0.08645064 0.10625716]\n",
      "Bu= -0.03477833430856342\n",
      "Bi= -0.19609159910738416\n",
      "已走90步,用了5000条训练数据\n",
      "Puk= [ 0.02699231  0.01933262  0.02243716 -0.00851376  0.02754147  0.00644056\n",
      "  0.01148979  0.01718132  0.01491857 -0.00706913  0.00053374  0.00486893\n",
      "  0.02196501  0.01127785  0.03519059 -0.02018639  0.01595526  0.00561899\n",
      "  0.00327882  0.03075946]\n",
      "Qki= [-0.00216739  0.05289274  0.13087803  0.07982902  0.03691809  0.04127471\n",
      "  0.11487889  0.11465574  0.02862997  0.12056055  0.09571437  0.12760487\n",
      "  0.07343625  0.02336395  0.03880328  0.09214378  0.01575684  0.11992838\n",
      "  0.01389662  0.10494586]\n",
      "Bu= -0.02503898854932658\n",
      "Bi= -0.22658988836139463\n",
      "已走90步,用了10000条训练数据\n",
      "Puk= [-1.13372385e-02  1.25440764e-02 -2.45247922e-03 -1.87768963e-03\n",
      "  7.20404968e-03 -1.87953642e-02 -6.59971620e-03  3.93697099e-03\n",
      " -5.59013174e-03 -1.29346567e-02 -1.34193795e-02 -1.35416694e-02\n",
      "  9.77740730e-04 -5.54789710e-03 -3.95588782e-05  2.11309078e-03\n",
      "  4.38791620e-03 -4.06773675e-03  1.95678277e-02 -3.36351289e-03]\n",
      "Qki= [ 0.01637228  0.05487103  0.18687624  0.06490231  0.15555416 -0.00900623\n",
      " -0.01438973  0.10467358  0.117286    0.12903922  0.09828686  0.03624092\n",
      "  0.06241735  0.05586405  0.13287375  0.26555623  0.19891201  0.13016619\n",
      "  0.21594877  0.10401579]\n",
      "Bu= -0.016876033646910298\n",
      "Bi= 0.6474570082253568\n",
      "已走90步,用了15000条训练数据\n",
      "Puk= [ 0.04533628 -0.05565285  0.01833262 -0.00354708  0.00950336  0.05078585\n",
      " -0.00476767 -0.03063358  0.0402243   0.01622574  0.0299642   0.00850391\n",
      "  0.00060043  0.07031463  0.02638669  0.04337004 -0.03663966  0.0539888\n",
      "  0.0154363   0.06793495]\n",
      "Qki= [ 0.07811052  0.23872184  0.12330491 -0.0024142   0.08229352 -0.05464355\n",
      "  0.08668127  0.14707518  0.00851372  0.13547088  0.21557751  0.06658564\n",
      "  0.21819576 -0.06240154  0.08990925  0.19233007  0.01106364  0.12147269\n",
      "  0.0656797   0.08724994]\n",
      "Bu= 0.12298311652552454\n",
      "Bi= -0.3448565229893896\n",
      "SVD trained\n",
      "生成训练数据...\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\lib\\site-packages\\scipy\\spatial\\distance.py:644: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  dist = 1.0 - uv / np.sqrt(uu * vv)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train.csv:500 (userId, eventId)=(123290209, 1887085024)\n",
      "train.csv:1000 (userId, eventId)=(272886293, 199858305)\n",
      "train.csv:1500 (userId, eventId)=(395305791, 1582270949)\n",
      "train.csv:2000 (userId, eventId)=(527523423, 3272728211)\n",
      "train.csv:2500 (userId, eventId)=(651258472, 792632006)\n",
      "train.csv:3000 (userId, eventId)=(811791433, 524756826)\n",
      "train.csv:3500 (userId, eventId)=(985547042, 1269035551)\n",
      "train.csv:4000 (userId, eventId)=(1107615001, 173949238)\n",
      "train.csv:4500 (userId, eventId)=(1236336671, 3849306291)\n",
      "train.csv:5000 (userId, eventId)=(1414301782, 2652356640)\n",
      "train.csv:5500 (userId, eventId)=(1595465532, 955398943)\n",
      "train.csv:6000 (userId, eventId)=(1747091728, 2131379889)\n",
      "train.csv:6500 (userId, eventId)=(1914182220, 955398943)\n",
      "train.csv:7000 (userId, eventId)=(2071842684, 1076364848)\n",
      "train.csv:7500 (userId, eventId)=(2217853337, 3051438735)\n",
      "train.csv:8000 (userId, eventId)=(2338481531, 2525447278)\n",
      "train.csv:8500 (userId, eventId)=(2489551967, 520657921)\n",
      "train.csv:9000 (userId, eventId)=(2650493630, 87962584)\n",
      "train.csv:9500 (userId, eventId)=(2791418962, 4223848259)\n",
      "train.csv:10000 (userId, eventId)=(2903662804, 2791462807)\n",
      "train.csv:10500 (userId, eventId)=(3036141956, 3929507420)\n",
      "train.csv:11000 (userId, eventId)=(3176074542, 3459485614)\n",
      "train.csv:11500 (userId, eventId)=(3285425249, 2271782630)\n",
      "train.csv:12000 (userId, eventId)=(3410667855, 1063772489)\n",
      "train.csv:12500 (userId, eventId)=(3531604778, 2584839423)\n",
      "train.csv:13000 (userId, eventId)=(3686871863, 53495098)\n",
      "train.csv:13500 (userId, eventId)=(3833637800, 2415873572)\n",
      "train.csv:14000 (userId, eventId)=(3944021305, 2096772901)\n",
      "train.csv:14500 (userId, eventId)=(4075466480, 3567240505)\n",
      "train.csv:15000 (userId, eventId)=(4197193550, 1628057176)\n",
      "生成预测数据...\n",
      "\n",
      "test.csv:500 (userId, eventId)=(182290053, 2529072432)\n",
      "test.csv:1000 (userId, eventId)=(433510318, 4244463632)\n",
      "test.csv:1500 (userId, eventId)=(632808865, 2845303452)\n",
      "test.csv:2000 (userId, eventId)=(813611885, 2036538169)\n",
      "test.csv:2500 (userId, eventId)=(1010701404, 303459881)\n",
      "test.csv:3000 (userId, eventId)=(1210932037, 2529072432)\n",
      "test.csv:3500 (userId, eventId)=(1452921099, 2705317682)\n",
      "test.csv:4000 (userId, eventId)=(1623287180, 1626678328)\n",
      "test.csv:4500 (userId, eventId)=(1855201342, 2603032829)\n",
      "test.csv:5000 (userId, eventId)=(2083900381, 2529072432)\n",
      "test.csv:5500 (userId, eventId)=(2318415276, 2509151803)\n",
      "test.csv:6000 (userId, eventId)=(2528161539, 4025975316)\n",
      "test.csv:6500 (userId, eventId)=(2749110768, 4244406355)\n",
      "test.csv:7000 (userId, eventId)=(2927772127, 1532377761)\n",
      "test.csv:7500 (userId, eventId)=(3199685636, 1776393554)\n",
      "test.csv:8000 (userId, eventId)=(3393388475, 680270887)\n",
      "test.csv:8500 (userId, eventId)=(3601169721, 154434302)\n",
      "test.csv:9000 (userId, eventId)=(3828963415, 3067222491)\n",
      "test.csv:9500 (userId, eventId)=(4018723397, 2522610844)\n",
      "test.csv:10000 (userId, eventId)=(4180064266, 2658555390)\n"
     ]
    }
   ],
   "source": [
    "RS = RecommonderSystem()\n",
    "print(\"生成训练数据...\\n\") \n",
    "generateRSData(RS,train=True,  header=True)\n",
    "\n",
    "print(\"生成预测数据...\\n\") \n",
    "generateRSData(RS, train=False, header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
